// ==UserScript==
// @name         Bilibili视频自动跳过（片头片尾）
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  自动跳过视频开头和结尾指定时长，支持合集播放
// @author       Your name
// @match        *://*.bilibili.com/video/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 设置要跳过的秒数
    const SKIP_START_SECONDS = 5; // 跳过片头时长（秒）
    const SKIP_END_SECONDS = 90;  // 跳过片尾时长（秒）
    
    // 存储事件监听器状态
    let currentVideo = null;
    let hasAttachedListeners = false;

    function attachVideoListeners(video) {
        if (!video || video === currentVideo) return;
        
        // 清除旧的事件监听器
        if (currentVideo) {
            currentVideo.removeEventListener('loadeddata', handleLoadedData);
            currentVideo.removeEventListener('play', handlePlay);
            currentVideo.removeEventListener('timeupdate', handleTimeUpdate);
        }

        currentVideo = video;
        hasAttachedListeners = true;

        // 添加新的事件监听器
        video.addEventListener('loadeddata', handleLoadedData);
        video.addEventListener('play', handlePlay);
        video.addEventListener('timeupdate', handleTimeUpdate);
        
        console.log('已添加视频事件监听器');
    }

    function handleLoadedData() {
        if (this.currentTime < SKIP_START_SECONDS) {
            this.currentTime = SKIP_START_SECONDS;
            console.log(`已自动跳过片头${SKIP_START_SECONDS}秒`);
        }
    }

    function handlePlay() {
        if (this.currentTime < SKIP_START_SECONDS) {
            this.currentTime = SKIP_START_SECONDS;
            console.log(`已自动跳过片头${SKIP_START_SECONDS}秒`);
        }
    }

    function handleTimeUpdate() {
        const remainingTime = this.duration - this.currentTime;
        if (remainingTime <= SKIP_END_SECONDS && remainingTime > 0) {
            const nextButton = document.querySelector('.bilibili-player-video-btn-next');
            if (nextButton) {
                console.log(`距离视频结束还有${remainingTime.toFixed(1)}秒，自动跳转下一集`);
                nextButton.click();
            } else {
                this.currentTime = this.duration;
                console.log('已跳过片尾');
            }
        }
    }

    function findAndAttachVideo() {
        const video = document.querySelector('.bpx-player-video-wrap video, .bilibili-player-video video');
        if (video && !hasAttachedListeners) {
            attachVideoListeners(video);
        }
    }

    // 使用定时器确保视频元素加载
    function initializeVideoCheck() {
        // 立即检查一次
        findAndAttachVideo();

        // 设置定期检查
        setInterval(() => {
            findAndAttachVideo();
        }, 1000); // 每秒检查一次
    }

    // 监听页面URL变化（用于合集播放切换视频时）
    let lastUrl = location.href;
    new MutationObserver(() => {
        if (location.href !== lastUrl) {
            lastUrl = location.href;
            console.log('检测到视频切换');
            hasAttachedListeners = false;
            findAndAttachVideo();
        }
    }).observe(document, {subtree: true, childList: true});

    // 页面加载完成后执行
    if (document.readyState === 'complete') {
        initializeVideoCheck();
    } else {
        window.addEventListener('load', initializeVideoCheck);
    }
})(); 
